import BaseItemView from "../../cfw/mvc/BaseItemView";
import { ResType } from "../../cfw/res/ResInterface";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import ResItem from "../../cfw/res/ResItem";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName, ActionType, FaceType } from "../../public/Config";
import Player from "./Player";
import GrowItemModel from "./model/GrowItemModel";


const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerView extends BaseItemView {


    @property(cc.Node)
    head: cc.Node = null;

    @property([cc.Node])
    ears: cc.Node[] = []

    @property([cc.Sprite])
    clothes: cc.Sprite[] = []

    @property([cc.Node])
    arms: cc.Node[] = []

    @property([cc.Node])
    hairBack: cc.Node[] = []

    @property([cc.Node])
    hairTop: cc.Node[] = []

    // @property(cc.Node)
    // egg: cc.Node = null;

    @property([cc.Node])
    mouth: cc.Node[] = []

    @property(cc.Node)
    wings: cc.Node = null;

    @property([cc.Node])
    tails: cc.Node[] = []

    @property([cc.Node])
    eyelids: cc.Node[] = []

    @property(cc.Node)
    eyelid: cc.Node = null;

    @property(cc.Node)
    leg: cc.Node = null;



    // protected isShellOpen: number = 1;

    protected model: Player;

    protected initPos: cc.Vec2[] = []

    protected ignoreAdge: number = 1;

    // setShellOpen(n: number) {
    //     this.isShellOpen = n;
    // }

    private faceType: FaceType = FaceType.OPEN_SMILE;

    setFaceType(t: FaceType) {
        this.mouth[this.faceType].active = false;
        this.faceType = t;
        this.mouth[t].active = true;
    }

    setIgnoreAdge(n: number) {
        this.ignoreAdge = n;
    }
    start() {
        super.start();
        this.model = Player.instance();
        let clotheList = this.model.getClotheList()
        for (let index = 0; index < this.clothes.length; index++) {

            this.initPos[index] = cc.v2()
            this.initPos[index].x = this.clothes[index].node.x;
            this.initPos[index].y = this.clothes[index].node.y;
            // if(element > 0){
            const element = clotheList[index];
            this.updateClothes(index, element)
            // }

        }
        GlobalEvent.instance().on(EventName.SELECT_CLOTHE, this.updateClothes, this)
        GlobalEvent.instance().on(EventName.CHANGE_LEVEL, this.changeLevel, this)

        GlobalEvent.instance().on(EventName.CHANGE_FACE_TYPE, this.setFaceType, this)
        this.changeLevel()
        this.schedule(this.updateTime, 1)
        this.setFaceType(this.faceType)
      
    }

    // brokenShell() {
    //     this.shell.active = false;
    // }

    updateTime() {
        this.model.updateTime(1)
    }

    changeLevel() {
        let growItem: GrowItemModel = this.model.getGrowItemModel();
        if (!growItem) {
            cc.error(' growITem is null ')
            return;
        }
        if (this.ignoreAdge) {
            // this.egg.active = false;
        } else {
            // if (growItem.getFront() < 0) {
            //     this.egg.active = false
            // } else {
            //     this.egg.active = true;
            // }
            if (growItem.getLeg() < 0) {
                this.leg.active = false
            } else {
                this.leg.active = true;
            }
            // if (this.isShellOpen) {
            //     this.shell.active = false;
            // }
        }


        if (growItem.getWings() < 0) {
            this.wings.active = false
        } else {
            this.wings.active = true;
        }



        for (let index = 0; index < this.hairBack.length; index++) {
            const element = this.hairBack[index];
            if (index == growItem.getHairBack()) {
                element.active = true;
            } else {
                element.active = false;
            }
        }

        for (let index = 0; index < this.hairTop.length; index++) {
            const element = this.hairTop[index];
            if (index == growItem.getHairTop()) {
                element.active = true;
            } else {
                element.active = false;
            }
        }


        for (let index = 0; index < this.tails.length; index++) {
            const element = this.tails[index];
            if (index == growItem.getTail()) {
                element.active = true;
            } else {
                element.active = false;
            }
        }
    }


    onComplate() {

    }

    setArmIndex(armIndex: number) {
        for (let index = 0; index < this.arms.length; index++) {
            const element = this.arms[index];
            if (armIndex == index) {
                element.active = true;
            }
        }
    }

    onDestroy() {
        super.onDestroy();
        this.unschedule(this.updateTime)
        GlobalEvent.instance().off(EventName.CHANGE_FACE_TYPE, this.setFaceType, this)
        GlobalEvent.instance().off(EventName.SELECT_CLOTHE, this.updateClothes, this)
        GlobalEvent.instance().off(EventName.CHANGE_LEVEL, this.changeLevel, this)
    }

    updateClothes(index, clotheID) {
        // let type: number = clothe.getClotheType()
        // let index = type - 1;
        // cc.log(' index ', index)
        this.clothes[index].node.x = this.initPos[index].x;
        this.clothes[index].node.y = this.initPos[index].y;

        if (clotheID > 0) {
            let clothe = this.model.getClothe(clotheID)
            let offset = clothe.getOffset()
            if (offset && offset.length > 0) {
                this.clothes[index].node.x += offset[0]
                this.clothes[index].node.y += offset[1]
            }
            ModuleManager.getLoader(ModuleID.DECORATION).loadRes(clothe.getMask(), ResType.SpriteFrame, (err, res: ResItem) => {
                if (err || !cc.isValid(this.node)) {
                    return;
                }
                this.clothes[index].spriteFrame = res.getRes()
                // cc.log(' this.clothes[index] scale ', this.clothes[index].node.scale)
            })
        } else {
            if (this.model.canDel(index)) {
                this.clothes[index].spriteFrame = null;
            }

        }

    }
    // update (dt) {}
}
